import { drawerConfig, drawerTitle, drawerVisible } from "./drawer";
import { modalConfig } from "./modal";
import { searchConfig } from "./search";
import { tableData, tableConfig, tablePage, tableLoading } from "./table";
import { operateType, queryParams } from "./store";
import { fetchDataById, fetchSearchMap } from "./utils";
export const fetchList = async () => {
  try {
    tableData.value.length = 0;
    tableLoading.value = true;
    queryParams.page = tablePage.current;
    queryParams.page_size = tablePage.pageSize;
    const res: any = await payApi.query(queryParams);
    if (res.code != 200) return message.error(res.message);
    tableData.value = res?.data?.order_list ?? [];
    tablePage.total = res?.data?.attributes?.total ?? 0;
  } catch (error: any) {
    message.error(error.message);
  } finally {
    tableLoading.value = false;
  }
};
export const refreshTable = async (searchParams: any = {}) => {
  tablePage.current = 1;
  initObjAttr(queryParams, searchParams);
  await fetchList();
};
const showDrawer = () => (drawerVisible.value = true);
export const handleAction = async ({ record, type }: any) => {
  operateType.value = type;
  switch (trimAll(type)) {
    case "view":
      drawerTitle.value = $t("base.payMemberDetail");
      await fetchDataById({
        order_id: record.id,
      });
      showDrawer();
      break;
  }
};
export default defineComponent({
  setup() {
    const defineModal = modalConfig();
    const defineSearch = searchConfig();
    const defineTable = tableConfig();
    const defineDrawer = drawerConfig();
    onActivated(() => {
      fetchList();
      fetchSearchMap();
    });

    return () => (
      <>
        <s-design
          defineSearch={defineSearch}
          defineTable={defineTable}
          defineDrawer={defineDrawer}
          defineModal={defineModal}
        />
      </>
    );
  },
});
